home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / shllutil.lha / shellutils-1.8 / src / uname.c < prev    next >
C/C++ Source or Header  |  1991-12-07  |  4KB  |  156 lines

  1. /* uname -- print system information
  2.    Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Option        Example
  19.  
  20.    -s, --sysname    SunOS
  21.    -n, --nodename    rocky8
  22.    -r, --release    4.0
  23.    -v, --version    
  24.    -m, --machine    sun
  25.    -a, --all        SunOS rocky8 4.0  sun
  26.  
  27.    The default behavior is equivalent to `-s'.
  28.  
  29.    David MacKenzie <djm@ai.mit.edu> */
  30.  
  31. #include <stdio.h>
  32. #include <sys/types.h>
  33. #include <sys/utsname.h>
  34. #include <getopt.h>
  35. #include "system.h"
  36.  
  37. void error ();
  38. void print_element ();
  39. void usage ();
  40.  
  41. /* Values that are bitwise or'd into `toprint'. */
  42. /* Operating system name. */
  43. #define PRINT_SYSNAME 1
  44.  
  45. /* Node name on a communications network. */
  46. #define PRINT_NODENAME 2
  47.  
  48. /* Operating system release. */
  49. #define PRINT_RELEASE 4
  50.  
  51. /* Operating system version. */
  52. #define PRINT_VERSION 8
  53.  
  54. /* Machine hardware name. */
  55. #define PRINT_MACHINE 16
  56.  
  57. /* Mask indicating which elements of the name to print. */
  58. unsigned char toprint;
  59.  
  60. /* The name this program was run with, for error messages. */
  61. char *program_name;
  62.  
  63. struct option long_options[] =
  64. {
  65.   {"sysname", 0, NULL, 's'},
  66.   {"nodename", 0, NULL, 'n'},
  67.   {"release", 0, NULL, 'r'},
  68.   {"version", 0, NULL, 'v'},
  69.   {"machine", 0, NULL, 'm'},
  70.   {"all", 0, NULL, 'a'},
  71.   {NULL, 0, NULL, 0}
  72. };
  73.  
  74. void
  75. main (argc, argv)
  76.      int argc;
  77.      char **argv;
  78. {
  79.   struct utsname name;
  80.   int c;
  81.  
  82.   program_name = argv[0];
  83.   toprint = 0;
  84.  
  85.   while ((c = getopt_long (argc, argv, "snrvma", long_options, (int *) 0))
  86.      != EOF)
  87.     {
  88.       switch (c)
  89.     {
  90.     case 's':
  91.       toprint |= PRINT_SYSNAME;
  92.       break;
  93.     case 'n':
  94.       toprint |= PRINT_NODENAME;
  95.       break;
  96.     case 'r':
  97.       toprint |= PRINT_RELEASE;
  98.       break;
  99.     case 'v':
  100.       toprint |= PRINT_VERSION;
  101.       break;
  102.     case 'm':
  103.       toprint |= PRINT_MACHINE;
  104.       break;
  105.     case 'a':
  106.       toprint = PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE |
  107.         PRINT_VERSION | PRINT_MACHINE;
  108.       break;
  109.     default:
  110.       usage ();
  111.     }
  112.     }
  113.  
  114.   if (optind != argc)
  115.     usage ();
  116.  
  117.   if (toprint == 0)
  118.     toprint = PRINT_SYSNAME;
  119.  
  120.   if (uname (&name) == -1)
  121.     error (1, errno, "cannot get system name");
  122.  
  123.   print_element (PRINT_SYSNAME, name.sysname);
  124.   print_element (PRINT_NODENAME, name.nodename);
  125.   print_element (PRINT_RELEASE, name.release);
  126.   print_element (PRINT_VERSION, name.version);
  127.   print_element (PRINT_MACHINE, name.machine);
  128.  
  129.   exit (0);
  130. }
  131.  
  132. /* If the name element set in MASK is selected for printing in `toprint',
  133.    print ELEMENT; then print a space unless it is the last element to
  134.    be printed, in which case print a newline. */
  135.  
  136. void
  137. print_element (mask, element)
  138.      unsigned char mask;
  139.      char *element;
  140. {
  141.   if (toprint & mask)
  142.     {
  143.       toprint &= ~mask;
  144.       printf ("%s%c", element, toprint ? ' ' : '\n');
  145.     }
  146. }
  147.  
  148. void
  149. usage ()
  150. {
  151.   fprintf (stderr, "\
  152. Usage: %s [-snrvma] [--sysname] [--nodename] [--release] [--version]\n\
  153.        [--machine] [--all]\n", program_name);
  154.   exit (1);
  155. }
  156.